package com.huike.stock_analysis.service;

import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.stock_analysis.dao.TechniqueDao;
import com.huike.stock_analysis.entity.Stock;
import com.huike.stock_analysis.entity.TechniqueInfo;

@Service
public class TechniqueService {
	@Autowired
	private TechniqueDao techniqueDao;
	
	private List<TechniqueInfo> techniqueInfos;
	private List<Stock> stocks;
	//保留四位小数
	private DecimalFormat df = new DecimalFormat("0.0000");

	
	
	public void setList(String code) {
		
			techniqueInfos = techniqueDao.queryTech(code);
		
	}

	public void setStocks(String code) {
		
			stocks = techniqueDao.dayK(code);
		
	}
	
	//基本信息
	public Stock base(String code) {
		return techniqueDao.baseInfo(code);
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * 均线
	 */
	
	public Map<String, String> ma(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			double[] ma = techniqueInfo.getMa();
			String[] maS = new String[3];
			
			
			for(int i = 0; i < ma.length; i++) {
				maS[i] = df.format(ma[i]);
			
			}
			map.put(techniqueInfo.getDate().toString(), maS[0] + "," + maS[1] + "," + maS[2]);
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * ema
	 */

	public Map<String, String> ema(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			double[] ma = techniqueInfo.getema();
			String[] emaS = new String[2];
			
			
			for(int i = 0; i < ma.length; i++) {
				emaS[i] = df.format(ma[i]);
			
			}
			map.put(techniqueInfo.getDate().toString(), emaS[0] + "," + emaS[1]);
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * boll
	 */

	public Map<String, String> boll(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			double[] ma = techniqueInfo.getBoll();
			String[] bollS = new String[3];
			
			
			for(int i = 0; i < ma.length; i++) {
				bollS[i] = df.format(ma[i]);
			
			}
			map.put(techniqueInfo.getDate().toString(), bollS[0] + "," + bollS[1] + ',' + bollS[2]);
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * 5日涨跌幅
	 */

	public Map<String, String> change5(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			/*
			double[] ma = techniqueInfo.getBoll();
			String[] change = new String[2];
			
			
			for(int i = 0; i < ma.length; i++) {
				change[i] = df.format(ma[i]);
			
			}
			*/
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getChange5()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * macd
	 */

	public Map<String, String> macd(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			/*
			double[] ma = techniqueInfo.getBoll();
			String[] change = new String[2];
			
			
			for(int i = 0; i < ma.length; i++) {
				change[i] = df.format(ma[i]);
			
			}
			*/
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getMacd()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * rsv
	 */

	public Map<String, String> rsv(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			/*
			double[] ma = techniqueInfo.getBoll();
			String[] change = new String[2];
			
			
			for(int i = 0; i < ma.length; i++) {
				change[i] = df.format(ma[i]);
			
			}
			*/
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getRsv()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * rsi
	 */

	public Map<String, String> rsi(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			/*
			double[] ma = techniqueInfo.getBoll();
			String[] change = new String[2];
			
			
			for(int i = 0; i < ma.length; i++) {
				change[i] = df.format(ma[i]);
			
			}
			*/
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getRsi()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * kdj
	 */

	public Map<String, String> kdj(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			double[] ma = techniqueInfo.getKdj();
			String[] kdjS = new String[3];
			
			
			for(int i = 0; i < ma.length; i++) {
				kdjS[i] = df.format(ma[i]);
			
			}
			
			map.put(techniqueInfo.getDate().toString(), kdjS[0] + ',' + kdjS[1] + ',' + kdjS[2]);
			
		}
		return map;
	}
	/**
	 * 
	 * @param code
	 * @return
	 * dif
	 */

	public Map<String, String> dif(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			
			
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getDif()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * 成交量
	 */

	public Map<String, String> cjl(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			
			
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getCjl()));
			
		}
		return map;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 * 历史波动率
	 */

	public Map<String, String> historyRatio(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setList(code);
		
		for(TechniqueInfo techniqueInfo : techniqueInfos) {
			//取四位小数
			
			
			
			map.put(techniqueInfo.getDate().toString(), df.format(techniqueInfo.getHistoryRatio()) + "," + df.format(techniqueInfo.getAvrHistoryRatio()));
			
		}
		return map;
	}
	

	/**
	 * 
	 * @param code
	 * @return
	 * 日k
	 */

	public Map<String, String> dayK(String code){
		Map<String, String> map = new LinkedHashMap<>();
		setStocks(code);
		for (Stock stock : stocks) {
			map.put(stock.getDate(), stock.getOpen() + "," + stock.getClose()
					+ "," + stock.getHight() + "," + stock.getLow());
		}
		return map;
	}
	
	
	
	

}
